//
//  MCOneMonthSelectVc.swift
//  MCBodyBuildingPro
//
//  Created by Renlvshijie on 2023/6/5.
//

import UIKit
import SnapKit
import QMUIKit




class MCOneMonthSelectVc: MCBaseVc {
    
    
    var ids:String = ""
    
    
    private let mcOneMonthSelectCollectionIdentifier = "mcOneMonthSelectCollectionIdentifier"
    
    private let mcOneMonthSelectCollectionReusableIdentifier = "mcOneMonthSelectCollectionReusableIdentifier"
    
    @IBOutlet weak var selectCollectionView: UICollectionView!
    
    //日历
    var dateCalendar:NSCalendar!
    
    //当前日期
    var dateNow:Date!
    
    
    //月
    var monthCalendar:NSCalendar!
    
    //天
    var dayCalendar:NSCalendar!
    
    //年
    var yearCalendear:NSCalendar!
    
    var timeDataSource:Array = [Int]()
    
    var monthDataSource:Array = [MCDayMonthsYearModl?]()
    
    var networkDataSource:Array = [MCPlanTimePlanDateList?]()
    
    var selectDateDataSource:Array = [Int?]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.qmui_color(withHexString: "#F7F7F9")
        self.navigationItem.title = "运动日历"
     
        
        dateNow = Date.init()
        dateCalendar = NSCalendar.init(calendarIdentifier: .gregorian)
        dateCalendar = NSCalendar.current as NSCalendar
        let timeZone = TimeZone(identifier: "Asia/Shanghai")
        dateCalendar.timeZone = timeZone!
        

        let dateFlowLayout = UICollectionViewFlowLayout.init()
        dateFlowLayout.minimumLineSpacing = 0
        dateFlowLayout.minimumInteritemSpacing = 0
        dateFlowLayout.itemSize = CGSize(width: ScreenWidth / 7.0, height: ScreenWidth / 7.0)
        self.selectCollectionView.dataSource = self
        self.selectCollectionView.delegate = self
        self.selectCollectionView.setCollectionViewLayout(dateFlowLayout, animated: true)
        
        self.selectCollectionView.register(UINib(nibName: "MCOneMothsSelctCollectionCell", bundle: Bundle.main), forCellWithReuseIdentifier: mcOneMonthSelectCollectionIdentifier)
        self.selectCollectionView.register(MCDatePlayReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: mcOneMonthSelectCollectionReusableIdentifier)
        self.selectCollectionView.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "footerView")

    }
    
    
    
    //日
    func getDays(_ section: Int) -> Int {
        if getmonths() == 12 {
            if section == 1 {
                let daysnexts =  Date.jk.monthOfDays(month: section + 1)
                return daysnexts
            }else{
                let daysnexts =  Date.jk.monthOfDays(month:  getmonths() + section)
                return daysnexts
            }
        }else {
            
            let daysnexts =  Date.jk.monthOfDays(month: getmonths() + section)
            
            let timeModel:Dictionary<String,Any> = ["years":getYears(),"months":(getmonths() + section),"days":(daysnexts + getweekDay(section:section)),"weakDay":getweekDay(section:section)]
            
            let dayMonthsModel = MCDayMonthsYearModl(JSON: timeModel)
            
            self.monthDataSource.append(dayMonthsModel)
            
            return daysnexts
        
        }
    }
    
    //月
    func getmonths() -> Int {
        let months = dateCalendar.component(NSCalendar.Unit.month, from: Date.jk.currentDate)
        return months
    }
    
    //年
    func getYears() -> Int {
        let years = dateCalendar.component(NSCalendar.Unit.year, from: Date.jk.currentDate)
        return years
    }
    
    
    //周
    func getweekDay(section:Int) -> Int {
        
        var weakDay = dateCalendar.components([.era,.year,.month,.day], from: Date.jk.currentDate)
        weakDay.day = 1
        weakDay.month = getmonths() + section
        
        let firstDayComponents = dateCalendar.components([.era,.year,.month,.day,.weekday], from:dateCalendar.date(from: weakDay)!)
        
        print(firstDayComponents as Any)
        
        let weakDayNext = firstDayComponents.weekday
        
      
        return weakDayNext! - 1
    }
    
    
    @IBAction func selectSender(_ sender: UIButton) {
        
        if self.selectDateDataSource.isEmpty == true {
            self.view.makeToast("请选择日期",position: .center)
            return
        }
        
        var dateArr:Array = [String]()
        for elementInfo in self.selectDateDataSource {
            let dateNowInfo = "\(getYears())-\(getmonths())-\(String(describing: elementInfo!))"
            dateArr.append(dateNowInfo)
            
        }
                
        
        let trainTimeVc = MCTrainTimeVc.init()
        trainTimeVc.ids = self.ids
        trainTimeVc.type = 0
        trainTimeVc.planDateArr = dateArr
        self.navigationController?.pushViewController(trainTimeVc, animated: true)
        
    }
}

extension MCOneMonthSelectVc:UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {
   
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
      return getDays(section) + getweekDay(section: section) - 1
        
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:MCOneMothsSelctCollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: mcOneMonthSelectCollectionIdentifier, for: indexPath) as! MCOneMothsSelctCollectionCell
        
        let time = self.monthDataSource[indexPath.section]
        
        let dateTimeInfo = (indexPath.row - getweekDay(section:indexPath.section)) + 2
        
        if dateTimeInfo > 0 {
            
            cell.titleTimeInfoLabel.text = "\(String(describing:dateTimeInfo))"
            cell.titleTimeInfoLabel.isHidden = false
            cell.backGrardView.backgroundColor = UIColor.qmui_color(withHexString: "#FFFFFF")
            
        }else{
            
            cell.titleTimeInfoLabel.isHidden = true
            cell.backGrardView.backgroundColor = UIColor.qmui_color(withHexString: "#F7F7F9")
        }
                            
       
        
        return cell
    }
 
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: 100, height: 50)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        return CGSize(width: 100, height: 0.1)
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let headerResuableView:UICollectionReusableView!
        
        if kind == UICollectionView.elementKindSectionHeader {
            let resuableView:MCDatePlayReusableView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: mcOneMonthSelectCollectionReusableIdentifier, for: indexPath) as! MCDatePlayReusableView
            let time = self.monthDataSource[indexPath.section]
            headerResuableView = resuableView
            
            resuableView.yearMonthLabel.text = "\(String(describing:(time?.years)!))年\(String(describing:(time?.months)!))月"
            
        }else {
            let resuableView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "footerView", for: indexPath)
            headerResuableView = resuableView
        }
        
        return headerResuableView
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let cell:MCOneMothsSelctCollectionCell = collectionView.cellForItem(at: indexPath) as! MCOneMothsSelctCollectionCell
        
       
        if  self.selectDateDataSource.contains(indexPath.row) {
        
            self.selectDateDataSource.remove(indexPath.row)
            
            cell.backGrardView.backgroundColor = UIColor.qmui_color(withHexString: "#FFFFFF")
            
        }else{
            self.selectDateDataSource.append(indexPath.row)
            cell.backGrardView.backgroundColor = UIColor.qmui_color(withHexString: "#35DE94")
        }
        
        
    }
    
    
}
